package com.ysd.order.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.javassist.Loader.Simple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ysd.common.entity.Orders;
import com.ysd.common.entity.Product;
import com.ysd.common.utils.OrdersVo;
import com.ysd.common.utils.PageVo;
import com.ysd.order.mapper.OrdersMapper;
import com.ysd.order.service.OrdersService;


@Service
public class OrdersServiceImpl implements OrdersService{
	
	@Autowired
	public  OrdersMapper  mapper;
	
	
	@Override
	public ArrayList<Orders> showAll(OrdersVo ordersVo) {
		// TODO Auto-generated method stub
		return mapper.showAll(ordersVo);
	}

	@Override
	public int showCounts(OrdersVo ordersVo) {
		// TODO Auto-generated method stub
		return mapper.showCounts(ordersVo);
	}

	@Override
	public int update(Orders o) {
		// TODO Auto-generated method stub
		return mapper.update(o);
	}

	@Override
	public int updateFH(int id) {
		// TODO Auto-generated method stub
		return mapper.updateFH(id);
	}

	@Override
	public int insert(Orders orders) {
		// TODO Auto-generated method stub
		return mapper.insert(orders);
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return mapper.delete(id);
	}

	@Override
	public Map<String,Object> showInfoById(int id) {
		/* ext1中测试的json字符串格式 
		 *  [{"id":"4","name":"华为p40","f_pcid":"4","oldPrice":"4988","newPrice":"4188","plate":"华为","size":"166*166*14","cate":"智能","factory":"深圳","description":"超大电池","imgZT":"picture/p1.jpg","isXJ":"1","isDel":"0","weight":"1","isXSS":"0","isXSG":"0","ext1":"","ext2":"","ext3":"","ext4":"","ext5":"","ext6":""},{"id":"4","name":"华为p40","f_pcid":"4","oldPrice":"4988","newPrice":"4188","plate":"华为","size":"166*166*14","cate":"智能","factory":"深圳","description":"超大电池","imgZT":"picture/p1.jpg","isXJ":"1","isDel":"0","weight":"1","isXSS":"0","isXSG":"0","ext1":"","ext2":"","ext3":"","ext4":"","ext5":"","ext6":""}]
		 *  */

		String ext1 = mapper.showInfoById(id);
		Gson gson = new Gson();
		//将jsonlist字符串转化为list对象
		List<Product> list= gson.fromJson(ext1, new TypeToken<List<Product>>() {}.getType());
		Map<String,Object> map = new HashMap();
		map.put("code", 0);
		map.put("data", list);
		map.put("msg", "查询成功");
		map.put("count", list.size());
		
		return map;
	}

	@Override
	public Map<String, Object> showAllInfo(OrdersVo ordersVo, int page, int limit) {
		
		//根据分页的PageVo实体处理分页的参数
		PageVo pVo = new PageVo();
		pVo.setLimit(limit);
		pVo.setPage(page);
		//将处理好的pVo对象赋给ordersVo中的pageVo对象
		ordersVo.setVo(pVo);
		//查询所订单信息
		ArrayList<Orders> lists = showAll(ordersVo);
		//查询条件下的产品的总条数
		int num = showCounts(ordersVo);
		Map<String, Object> map = new HashMap();
		map.put("code", 0);
		map.put("data", lists);
		map.put("count", num);
		map.put("msg", "");
		
		return map;
	}

	@Override
	public Map<String,Object> showAllByFuKuanTime() {
		// TODO Auto-generated method stub
		/*
		 * 返回json格式为：   {"money":[9999.0,556.0,32111.0,563111.0,2222.0,1231.0,3899.0],"time":["2020-07-17","2020-07-16","2020-07-15","2020-07-14","2020-07-13","2020-07-12","2020-07-11"]}
		 */
		Map<String,Object> map = new HashMap();
		
		List<Orders> lists = mapper.showAllByFuKuanTime();
		ArrayList<Object> moneys = new ArrayList<Object>(); //声明一个list存储近7天的金额
		ArrayList<Object> times = new ArrayList<Object>(); //声明一个list存储近7天的日期
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//将order对象里面的数据取出来单独存储
		for(int i =0; i<lists.size();i++) {
			moneys.add(lists.get(i).getMoney());
			times.add(sdf.format(lists.get(i).getFukuantime()));
		}
		
		map.put("money", moneys);
		map.put("time", times);
		
		return map;
	}

	@Override
	public int updateFK(int id) {
		// TODO Auto-generated method stub
		
		return mapper.updateFK(id);
	}


}
